﻿using HY.Common;
using HY.Entity;
using HY.Service;
using HY.Web.Controllers;
using HY.Web.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HY.Web.Areas.Resource.Controllers
{
    public class ParkController : BaseController
    {
        ParkService service = new ParkService();
        // GET: Resource/Park
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Walk()
        {
            return View();
        }

        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult List(int page, int limit)
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var code = Request.Form["Code"];
                var where = "P.IsDeleted=0";
                if (!string.IsNullOrWhiteSpace(code))
                    where += $" AND P.Code like '%{code}%'";
                var tuple = service.GetPageList<V_ParkList>("Park P JOIN SysDept S ON P.DeptID=S.ID", "P.*,S.DeptName", where, page, limit, "P.CreateDate desc");
                returnModel = base.GetReturnModel(tuple.Item1, tuple.Item2);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error(new LogInfo() { UserID = UserInfo.UserID, LogType = LogType.Select, LogMessage = "查询园区列表" }, ex);
            }
            return Json(returnModel);
        }

        public ActionResult Add()
        {
            SysDeptService sysDeptService = new SysDeptService();
            ViewBag.DeptList = JsonConvert.SerializeObject(sysDeptService.EFGetMany<SysDept>(s => !s.IsDeleted && s.DeptType == 0).Select(s => new { s.ID, s.DeptName }));
            return View();
        }
        [HttpPost]
        public JsonResult Add(Park model)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Insert };
            try
            {
                model.CreateUserID = base.UserInfo.UserID;
                model.CreateDate = DateTime.Now;
                returnModel.count = service.Add(model);
                returnModel.msg = "保存成功";
                logInfo.LogMessage = $"园区{returnModel.msg}";
                LogHelper.Info(logInfo);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "保存失败";
                logInfo.LogMessage = $"园区{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }

        public ActionResult Edit(int? id)
        {
            SysDeptService sysDeptService = new SysDeptService();
            ViewBag.DeptList = JsonConvert.SerializeObject(sysDeptService.EFGetMany<SysDept>(s => !s.IsDeleted && s.DeptType == 0).Select(s => new { s.ID, s.DeptName }));
            var model = service.GetById<Park>(id);
            return View(model);
        }

        [HttpPost]
        public JsonResult Edit(Park model)
        {
            ReturnModel returnModel = new ReturnModel();
            LogInfo logInfo = new LogInfo() { UserID = base.UserInfo.UserID, LogType = LogType.Update };
            try
            {
                var oldModel = service.GetById<Park>(model.ID);
                model.IsDeleted = oldModel.IsDeleted;
                model.CreateDate = oldModel.CreateDate;
                model.CreateUserID = oldModel.CreateUserID;
                model.LastModifyUserID = base.UserInfo.UserID;
                model.LastModifyDate = DateTime.Now;
                returnModel.count = service.Update(model);
                returnModel.msg = "保存成功";
                logInfo.LogMessage = $"园区{returnModel.msg}";
                LogHelper.Info(logInfo);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                returnModel.msg = "保存失败";
                logInfo.LogMessage = $"园区{returnModel.msg}";
                LogHelper.Error(logInfo, ex);
            }
            return Json(returnModel);
        }
        [HttpPost]
        [AllowAnonymous]
        public JsonResult SelectList()
        {
            ReturnModel returnModel = new ReturnModel();
            try
            {
                var list = service.EFGetMany<Park>(e => e.IsDeleted == false).Select(s => new { value = s.ID, text = s.Code });
                returnModel = GetReturnModel(list.Count(), list);
            }
            catch (Exception ex)
            {
                returnModel = base.GetReturnModel(ex);
                LogHelper.Error("获取园区下拉列表失败", ex);
            }
            return Json(returnModel);
        }
    }
}